<%@ page import="com.ws.ssmzongjie3.util.Res" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>编辑路线</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.min.css">
    <script src="${pageContext.request.contextPath}/bootstrap/js/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }
        .form-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            max-width: 500px;
            margin: 0 auto;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input, .form-group select, .form-group textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .form-group input[type="submit"] {
            background-color: #007bff;
            color: #fff;
            border: none;
            cursor: pointer;
        }
        .form-group input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <h2>编辑路线</h2>
        <form action="${pageContext.request.contextPath}/route/update" method="post" enctype="multipart/form-data">
            <input type="hidden" name="route_id" value="${route.route_id}">
            <input type="hidden" name="currentPage" value="${param.currentPage}">
            <div class="form-group">
                <label for="routeName">路线名称</label>
                <input type="text" id="routeName" name="route_name" value="${route.route_name}" required>
            </div>
            <div class="form-group">
                <label for="description">路线描述</label>
            </div>                <textarea id="description" name="description" rows="3" class="form-control">${route.description}</textarea>

            <div class="form-group">
                <label for="routeImage">路线图片</label>
                <input type="file" id="routeImage" name="image" accept="image/*">
                <!-- 图片预览区域 -->
                <div id="image-preview" style="margin-top: 10px;">
                    <c:if test="${not empty route.route_photo}">
                        <img src="<%=Res.fileUrl%>${route.route_photo}" width="100" height="100" alt="当前路线图片">
                    </c:if>
                </div>
            </div>
            <div class="form-group">
                <label for="routeStartPlace">出发地</label>
                <input type="text" id="routeStartPlace" name="route_start_place" value="${route.route_start_place}" required>
            </div>
            <div class="form-group">
                <label for="routeEndPlace">目的地</label>
                <input type="text" id="routeEndPlace" name="route_end_place" value="${route.route_end_place}" required>
            </div>
            <div class="form-group">
                <label for="routeDay">天数</label>
                <input type="number" id="routeDay" name="route_day" value="${route.route_day}" min="1" required>
            </div>
            <div class="form-group">
                <label for="routeType">类型</label>
                <select id="routeType" name="column_id" required>
                    <c:forEach items="${columns}" var="type">
                        <option value="${type.column_id}" <c:if test="${route.column_id == type.column_id}">selected</c:if>>${type.column_name}</option>
                    </c:forEach>
                </select>
            </div>
            <div class="form-group">
                <label for="sales">销量</label>
                <input type="number" id="sales" name="sales" value="${route.sales}" min="0" required>
            </div>
            <div class="form-group">
                <label for="recommend">推荐</label>
                <select id="recommend" name="recommend" required>
                    <option value="0" <c:if test="${route.recommend == 0}">selected</c:if>>推荐</option>
                    <option value="1" <c:if test="${route.recommend == 1}">selected</c:if>>不推荐</option>
                </select>
            </div>
            <div class="form-group">
                <label for="route_price">价格</label>
                <input type="number" id="route_price" name="route_price" value="${route.route_price}" min="0" required>
            </div>
            <div class="form-group">
                <label for="is_delete">状态</label>
                <select id="is_delete" name="is_delete" required>
                    <option value="0" <c:if test="${route.is_delete == 0}">selected</c:if>>上架</option>
                    <option value="1" <c:if test="${route.is_delete == 1}">selected</c:if>>下架</option>
                </select>
            </div>
            <div class="form-group">
                <label for="business_id">商家</label>
                <select id="business_id" name="business_id" required>
                    <c:forEach items="${businesses}" var="business">
                        <c:if test="${houuser.sid == 0 || business.business_id == houuser.sid}">
                            <option value="${business.business_id}" <c:if test="${route.business_id == business.business_id}">selected</c:if>>${business.business_name}</option>
                        </c:if>
                    </c:forEach>
                </select>
            </div>
            <div class="form-group">
                <label for="isrouteCarousel">是否轮播：</label>
                <select class="form-control" id="isrouteCarousel" name="carousel" required>
                    <option value="0" <c:if test="${route.carousel == 0}">selected</c:if>>是</option>
                    <option value="1" <c:if test="${route.carousel == 1}">selected</c:if>>否</option>
                </select>
            </div>
            <div class="form-group">
                <label for="route_special">是否特价：</label>
                <select class="form-control" id="route_special" name="route_special" required>
                    <option value="0" <c:if test="${route.route_special == 0}">selected</c:if>>是</option>
                    <option value="1" <c:if test="${route.route_special == 1}">selected</c:if>>否</option>
                </select>
            </div>
            <div class="form-group">

                <input type="submit" value="更新">
                <button type="button" class="btn btn-default" onclick="window.history.back()">返回</button>
            </div>
        </form>
    </div>

    <script>
        // 图片上传预览功能
        document.getElementById('routeImage').addEventListener('change', function(event) {
            var preview = document.getElementById('image-preview');
            preview.innerHTML = ''; // 清空之前的预览

            var files = event.target.files;
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                var reader = new FileReader();

                reader.onload = (function(file) {
                    return function(e) {
                        var img = document.createElement('img');
                        img.src = e.target.result;
                        img.style.width = '100px';
                        img.style.height = '100px';
                        img.style.margin = '5px';
                        preview.appendChild(img);
                    };
                })(file);

                reader.readAsDataURL(file);
            }
        });
    </script>
</body>
</html>
